package com.cryptoneedle.center.source.repository.metadata;


import com.bubbles.engine.data.core.repository.BaseRepository;
import com.cryptoneedle.center.source.entity.metadata.Server;
import com.cryptoneedle.center.source.key.metadata.ServerKey;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>description: 源数据源-服务器 </p>
 *
 * @author CryptoNeedle
 * @date 2025-06-20
 */
@Repository
public interface ServerRepository extends BaseRepository<Server, ServerKey> {

    @Query("FROM Server ORDER BY host, port")
    List<Server> servers();

    @Modifying
    @Query("""
            DELETE FROM Server s
             WHERE NOT EXISTS (SELECT 1
                                 FROM Catalog c
                                WHERE s.host = c.host
                                  AND s.port = c.port)""")
    void serversClean();
}